﻿using System.Data;

namespace FluentSql
{
    public class FluentCommandFactory : IFluentCommandFactory
    {
        #region Public Properties

        public CommandBehavior DefaultBehavior { get; protected set; }

        public CachingMode DefaultCaching { get; protected set; }

        public CommandType DefaultCommandType { get; protected set; }

        public IDalContext DeafultContext { get; protected set; }

        public IsolationLevel DefaultIsolationLevel { get; protected set; }

        public bool DefaultKeepConnection { get; protected set; }

        #endregion Public Properties

        #region Public Methods

        public IFluentCommandFactory SetCachingMode(CachingMode iCachingMode)
        {
            DefaultCaching = iCachingMode;
            return this;
        }

        public IFluentCommandFactory SetDefaultBehavior(CommandBehavior iBehavior)
        {
            DefaultBehavior = iBehavior;
            return this;
        }

        public IFluentCommandFactory SetDefaultCommandType(CommandType iCommandType)
        {
            DefaultCommandType = iCommandType;
            return this;
        }

        public IFluentCommandFactory SetDefaultContext(IDalContext iContext)
        {
            DeafultContext = iContext;
            return this;
        }

        public IFluentCommandFactory SetDefaultIsolationLevel(IsolationLevel iIsolationLevel)
        {
            DefaultIsolationLevel = iIsolationLevel;
            return this;
        }

        public IFluentCommandFactory SetDefaultKeepConnection(bool iKeepConnection)
        {
            DefaultKeepConnection = iKeepConnection;
            return this;
        }

        public INonQueryFluentSqlCommand NonQuery()
        {
            return new NonQueryFluentSqlCommand()
            {
                CommandType = DefaultCommandType,
                Context = DeafultContext,
                IsolationLevel = DefaultIsolationLevel,
                KeepConnection = DefaultKeepConnection
            };
        }

        public IScalarFluentSqlCommand Scalar()
        {
            return new ScalarFluentSqlCommand()
            {
                CommandType = DefaultCommandType,
                Context = DeafultContext,
                IsolationLevel = DefaultIsolationLevel,
                KeepConnection = DefaultKeepConnection
            };
        }

        public IReaderFluentSqlCommand<T> Reader<T>()
        {
            return new ReaderFluentSqlCommand<T>()
            {
                CommandType = DefaultCommandType,
                Context = DeafultContext,
                IsolationLevel = DefaultIsolationLevel,
                KeepConnection = DefaultKeepConnection,
                Behavior = DefaultBehavior,
                Caching = DefaultCaching
            };
        }

        #endregion Public Methods
    }
}
